package com.light.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.light.common.constants.Result;
import com.light.common.enums.ExceptionEnum;
import com.light.gulimall.member.dto.LoginDTO;
import com.light.gulimall.member.dto.SocialUser;
import com.light.gulimall.member.exception.PhoneExeception;
import com.light.gulimall.member.exception.UserNameException;
import com.light.gulimall.member.vo.UserRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.light.gulimall.member.entity.MemberEntity;
import com.light.gulimall.member.service.MemberService;
import com.light.common.utils.PageUtils;
import com.light.common.utils.R;



/**
 * 会员
 *
 * @author minmin
 * @email 980680541@gmail.com
 * @date 2024-07-16 21:23:05
 */
@RestController
@RequestMapping("/member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 用户注册
     * @param vo    注册信息
     * @return  注册结果
     */
    @PostMapping("/regist")
    public R regist(@RequestBody UserRegisterVo vo){

        try {
            memberService.userRegister(vo);
        } catch (UserNameException e) {
            return R.error(ExceptionEnum.USER_EXIST_EXCEPTION.getCode(),
                    ExceptionEnum.USER_EXIST_EXCEPTION.getValue());
        } catch (PhoneExeception e) {
            return R.error(ExceptionEnum.PHONE_EXIST_EXCEPTION.getCode(),
                    ExceptionEnum.PHONE_EXIST_EXCEPTION.getValue());
        }
        return R.ok();
    }

    /**
     * 普通登录
     * @param dto
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginDTO dto){
        return memberService.login(dto);
    }

    /**
     * 社交登录
     * @param dto
     * @return
     */
    @PostMapping("/socialLogin")
    public R login(@RequestBody SocialUser dto){
        return memberService.login(dto);
    }

}
